<template>
  <div class="login">
    <div class="login_box">
      <!-- 图标 -->
      <div class="avatar">
        <img src="../assets/logo.png" alt>
      </div>
      <!-- 登录表单 -->
      <el-form class="loginForm" ref="loginFormRef" :model="loginForm" :rules="rules">
        <el-form-item prop="username">
          <el-input prefix-icon="iconfont icon-yonghu" v-model="loginForm.username" ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" prefix-icon="iconfont icon-mima1" v-model="loginForm.password"></el-input>
        </el-form-item>
        <div class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm:{
        username:'admin',
        password:'123456'
      },
      rules:{
        username:[
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password:[
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 15, message: '长度在 5 到 15 个字符', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    login() {
      // 提交表单前先对照表单规则预验证  如果不对就不提交给后台 用插件validate方法
      // console.log(this.$refs.loginFormRef)//得到的是表单的实例  this指的是login.vue实例，下面有个$refs具体引用的dom元素或组件的引用
      this.$refs.loginFormRef.validate(async flag => {
        // console.log(flag)//表示是否校验成功
        // if(!flag) return
        if(flag) {
          // console.log('校验成功')//校验成功就发请求给后台验证用户名密码
          //请求地址必须是全称，如需简写 需要全局配置基准地址  baseURL
          // console.log(this.$http.post('/login',this.loginForm))//promise对象 使用await async
          const { data: res} = await this.$http.post('/login',this.loginForm)//解构赋值
          // console.log(res)
          if(res.meta.status == 200) {
            // Message组件插件来提示用户
            // console.log(res.meta.msg)
            this.$message({
              message: res.meta.msg,
              type: 'success'
            });
            //登录成功后将token值存在sessionStorage中
            sessionStorage.setItem('token',res.data.token)
            this.$router.push('/home')
          } else {
            this.$message({
              message: res.meta.msg,
              type: 'warning'
            });
          }
        }
        //不需要else  因为校验失败已经有提示了
      })
      // console.log(this.$http)
      // console.log(this.loginForm)
      
    },
    reset() {
      this.$refs.loginFormRef.resetFields()
    }
  },

  created() {}
};
</script>

<style lang='less' scoped>
.login {
  height: 100%;
  background: #2b4b6b;
  .login_box {
    position: absolute;
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 5px;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .avatar {
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 130px;
      height: 130px;
      padding: 5px;
      background: #fff;
      border-radius: 50%;
      border: 1px dashed #eee;
      box-shadow: 0 0 10px #ddd;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background: #eee;
      }
    }
    .btns {
      float: right;
    }
  }
  .el-form {
    position: absolute;
    bottom: 30px;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
  }
}
</style>
